package com.fengwk.support.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.fengwk.support.exception.BaseException;

import net.sourceforge.jeuclid.ParameterKey;
import net.sourceforge.jeuclid.converter.Converter;
import net.sourceforge.jeuclid.parser.Parser;
public class JeuclidUtils {
	
	private static Converter converter = Converter.getConverter();

	private JeuclidUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	public static byte[] mathMlToImage(String mathXml, Integer fontSize) throws SAXException, IOException, ParserConfigurationException {
		Document doc = Parser.getParser().parseStreamSource(new StreamSource(IoUtils.toInputStream(mathXml)));
		Map<ParameterKey, String> params = new HashMap<ParameterKey, String>();
        if(fontSize != null) params.put(ParameterKey.FontSize, fontSize + "");
        params.put(ParameterKey.OutFileType, "image/png");
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        converter.convert(doc, out , params);
        byte[] res = out.toByteArray();
        out.close();
        return res;
	}
	
}
